package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.FocusUser;
import com.itheima.service.mongo.FocusService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@Service
public class FocusServiceImpl implements FocusService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存关注信息
     * @param focusUser 关注对象
     */
    @Override
    public void saveFocus(FocusUser focusUser) {
        focusUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(focusUser);
    }

    /**
     * 取消关注用户
     * @param loginUserId 用户id
     * @param videoUserId 视频发布者id
     */
    @Override
    public void unFollowUser(Long loginUserId, Long videoUserId) {
        Query query = new Query(Criteria.where("userId").is(loginUserId)
                                .and("focusUserId").is(videoUserId));
        mongoTemplate.remove(query,FocusUser.class);
    }
}
